<?php


class fbMySQL {
	private $dbh;

	function __construct($host, $user, $pass) {
		$rtv = mysql_connect($host, $user, $pass);
		if($rtv != FALSE) {
			$this->dbh = $rtv;
		} else {
			return FALSE;
		}
	}

	function select_db($name)
	{
		return @mysql_select_db($name, $this->dbh);
	}

	function query($query) {
//		echo "Q: " . $query . "<br />";
		$rv = @mysql_query($query, $this->dbh);
//		echo "<pre>";
//		var_dump($rv);
//		echo "</pre>";
		if(is_resource($rv)) {
			$rtv = new fbMySQLResult($rv);
		} else {
			switch($rv) {
				case TRUE:
				case FALSE:
					$rtv = $rv;
					break;
				default:
					$rtv = NULL;
					break;
			}
		}
		return $rtv;
	}

	function affected_rows()
	{
		return mysql_affected_rows($this->dbh);
	}
	
	function insert_id() {
		return mysql_insert_id($this->dbh);
	}

	function escape($str) {
		return mysql_real_escape_string($str, $this->dbh);
	}
	
	function __destruct() {
		mysql_close($this->dbh);
	}
}

class fbMySQLResult {
	private $res;
	
	function __construct($res) {
		$this->res = $res;
	}

	function num_fields()
	{
		return mysql_num_fields($this->res);
	}

	function num_rows()
	{
		return mysql_num_rows($this->res);
	}
	
	function next_array() {
		return mysql_fetch_row($this->res);
	}
	
	function next_hash() {
		return mysql_fetch_assoc($this->res);
	}
	
	function seek($pos) {
		return mysql_data_seek($this->res, $pos);
	}
	
	function __destruct() {
		mysql_free_result($this->res);
	}
}

?>